package com.situ.day8.jdbc;

import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class JDBCDemo {

    @Test
    public void test222() {
        Connection connection = null;
        PreparedStatement statement = null;
        String sql1 = "update account set money=money-1000 where name='张三'";
        String sql2 = "update account set money=money+1000 where name='李四'";
        try {
            connection = JDBCUtil.getConnection();
            connection.setAutoCommit(false);
            statement = connection.prepareStatement(sql1);
            System.out.println(statement);
            int count1 = statement.executeUpdate();
            System.out.println("count1: " + count1);

            //int i = 3 / 0;

            statement = connection.prepareStatement(sql2);
            System.out.println(statement);
            int count2 = statement.executeUpdate();
            System.out.println("count2: " + count2);

            connection.commit();
        } catch (Exception throwables) {
            throwables.printStackTrace();

            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }



    @Test
    public void test1() {
        try {
            //1、加载驱动Class.forName("");
            Class.forName("com.mysql.jdbc.Driver");
            //2、获得连接对象Connection
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2303?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
            //3、写sql语句
            String sql = "select id,name,age,gender from student";
            //String name = "zhangsan' OR 1=1 -- y ";
            //String sql = "select id,name,age,gender from student where name='"+name+"'";
            //4、创建Statement(一艘船)
            Statement statement = connection.createStatement();
            //5、执行sql语句
            // (1) 更新类（更改了表里面数据）：delete/update/insert     executeUpdate()
            //返回值：int，表示你影响的行数
            // (2)查询（没有改变表里面数据）:  select                   executeQuery()
            //返回值：结果集ResultSet
            ResultSet resultSet = statement.executeQuery(sql);
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");

                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
            //6、关闭连接
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        // todo:关闭
    }

    @Test
    public void test3() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2303?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
            String sql = "select id,name,age,gender from student";
            //预编译Statement，这个时候就要传递过去sql语句
            PreparedStatement statement = connection.prepareStatement(sql);
            //打印要执行的sql语句
            System.out.println(statement);
            ResultSet resultSet = statement.executeQuery();
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");

                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Test
    public void test32() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java2303?useUnicode=true&characterEncoding=UTF-8", "root", "1234");
            String sql = "select id,name,age,gender from student";
            //预编译Statement，这个时候就要传递过去sql语句
            statement = connection.prepareStatement(sql);
            //打印要执行的sql语句
            System.out.println(statement);
            resultSet = statement.executeQuery();
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");

                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

    //public abstract List<Student> selectAll();
    //"select id,name,age,gender from student";

    @Test
    public void test37() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,age,gender from student";
            //预编译Statement，这个时候就要传递过去sql语句
            statement = connection.prepareStatement(sql);
            //打印要执行的sql语句
            System.out.println(statement);
            resultSet = statement.executeQuery();
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");

                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }
    }


    @Test
    public void test35() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            //String sql = "select id,name,age,gender from student where name=?";
            String sql = "select id,name,age,gender from student where name like ?";
            //预编译Statement，这个时候就要传递过去sql语句
            statement = connection.prepareStatement(sql);
            statement.setString(1, "张%");
            //打印要执行的sql语句
            System.out.println(statement);
            resultSet = statement.executeQuery();
            List<Student> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");

                Student student = new Student(id, name, age, gender);
                list.add(student);
            }
            for (Student student : list) {
                System.out.println(student);
            }
        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {//不管抛不抛出异常都会执行的代码
            JDBCUtil.close(connection, statement, resultSet);
        }
    }


    @Test
    public void testDelete() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from student where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, 6);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

    }

    @Test
    public void testInsert() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into student(name,age,gender) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "校招");
            statement.setInt(2, 34);
            statement.setString(3, "男");
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }

    }

    @Test
    public void testUpdate() {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update student set name=?,age=?,gender=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "小赵");
            statement.setInt(2, 24);
            statement.setString(3, "女");
            statement.setInt(4, 8);
            System.out.println(statement);
            int count = statement.executeUpdate();
            System.out.println("count: " + count);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, null);
        }
    }

    @Test
    public void test12() {
        String queryName = "三";
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Student> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,age,gender from student where name like ?";
            statement  = connection.prepareStatement(sql);
            statement.setString(1, "%"+queryName+"%");
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                Student student = new Student(id, name, age, gender);
                list.add(student);
            }

            for (Student student : list) {
                System.out.println(student);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtil.close(connection, statement, resultSet);
        }

    }




}
